﻿using Forum.BaseController;
using Forum.Model.Entity.System;
using Forum.Model.QueryFilters;
using Forum.Service.Interface.System;

namespace Forum.Controllers.System
{
    /// <summary>
    /// 用户角色
    /// </summary>
    [Route("Api/SysRole")]
    [ApiExplorerSettings(GroupName = nameof(ApiVersions.System))]
    public class SysRoleController : BaseController<ISysRoleService>
    {
        public SysRoleController(ISysRoleService defaultService, ILogger<BaseController<ISysRoleService>> logger) : base(defaultService, logger)
        {

        }

        #region 角色信息

        /// <summary>
        /// 分页查询角色
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpGet("[action]")]
        public ApiResponsePaged<SYS_ROLE> GetPagedList([FromQuery] QueryParameters param)
        {
            var pagedList = DefaultService.GetPagedList(param);
            return new ApiResponsePaged<SYS_ROLE>
            {
                msg = "Get Role list",
                data = pagedList.ToList(),
                total = pagedList.Total,
                haveMore = pagedList.Total > param.Page * param.PageSize
            };
        }

        #endregion

        #region 用户角色
        /// <summary>
        /// 查询用户的角色信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet("{userId}")]
        public ApiResponse<List<SYS_ROLE>> GetUserRoles(string? userId)
        {
            if (string.IsNullOrWhiteSpace(userId))
            {
                return ApiResponse.Error("未传入用户身份信息，请检查！");
            }

            return new ApiResponse<List<SYS_ROLE>>(DefaultService.GetUserRoles(userId));
        }

        /// <summary>
        /// 新增用户角色
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResponse AddUserRole(UserRoleReq req)
        {
            if (req == null || string.IsNullOrWhiteSpace(req.UserId) || req.RoleList == null || req.RoleList.Count == 0)
                return ApiResponse.Error("用户角色信息缺失，请检查！");

            var result = DefaultService.AddUserRole(req);
            return result ? ApiResponse.OK("新增完成") : ApiResponse.Error("新增用户信息异常，请稍后重试！");
        }

        #endregion
    }
}
